package com.xianzhiOA.controller;/**
 * Created by Administrator on 2017/5/26.
 */

import com.xianzhiOA.bean.XzEmp;
import com.xianzhiOA.bean.XzProject;
import com.xianzhiOA.bean.XzWeek;
import com.xianzhiOA.service.XzEmpService;
import com.xianzhiOA.service.XzProjectService;
import com.xianzhiOA.service.XzWeekService;
import com.xianzhiOA.service.XzWorkService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 工作量概况
 *
 * @author 甘汝雷
 * @create 2017-05-26 14:42
 **/
@Controller
@RequestMapping("/Week")
@SessionAttributes("userLogin")
public class XzWeekController {
    @Autowired
    private XzEmpService empService;
    @Autowired
    private XzWeekService weekService;
    @Autowired
    private XzWorkService workService;
    @Autowired
    private XzProjectService projectService;
    //添加工作量概况
    @RequestMapping("/addWeek.emp")
    public ModelAndView addWeek(XzWeek week,@ModelAttribute("userLogin")XzEmp emp) {
        ModelAndView mv = errors(week);
        if (mv.getViewName().equals("backEnd/weekEdit"))return mv;
        if (week.getEmpId()==null||week.getEmpId()==0)week.setEmpId(emp.getEmpId());
        week.setEmp(emp);
        weekService.insert(week);
        return mv;
    }

    //表单验证
    public ModelAndView errors(XzWeek week){
        ModelAndView mv = new ModelAndView("redirect:selWeekAll.emp");
        week.setWeekTime(weekService.myDate(week.getWeekString()));//把字符串转换成date类型
        Map errors = new HashMap();
        if (week.getWeekTime()!=null){
            Map byDate = workService.getWeekByDate(week.getWeekTime());
            byDate.put("empId",week.getEmpId());
            byDate.put("weekProject",week.getWeekProject());
            List<XzWeek> weekList = weekService.selAll(byDate);
            if (!weekList.isEmpty()) {
                if (week.getWeekId()==0) errors.put("weekTime","周期已存在不可添加");
                for (XzWeek w : weekList) {
                    if (week.getWeekId() != w.getWeekId()&&week.getWeekId()!=0) errors.put("weekTime","周期已存在不可修改");
                }
            }
        }else errors.put("weekTime","周期不能为空");
        if (!errors.isEmpty()){
            mv.addObject("errors", errors);
            mv.addObject("week", week);
            mv.addObject("projectList",projectService.selAll(new XzProject()));
            mv.addObject("empList",empService.selAll(new XzEmp()));
            mv.setViewName("backEnd/weekEdit");
            return mv;
        }
        return mv;
    }

    //分页查询工作量概况
    @RequestMapping("selWeekAll.emp")
    public ModelAndView selWeekAll(XzWeek week, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "8") int rows,
                                   Date begin, Date end, Date beginDate, Date endDate, @ModelAttribute("userLogin")XzEmp emp) {
        ModelAndView mv = new ModelAndView("backEnd/weekShow");
        week.setWeekTime(weekService.myDate(week.getWeekString()));
        if ((week.getEmpId()==null&&!emp.getEmpRole().equals("1"))&&(week.getEmpName()==""||week.getEmpName()==null))
            week.setEmpId(emp.getEmpId());
        PageBean<XzWeek> pageBean = weekService.selAll(week,page,rows,begin,end,beginDate,endDate);
        mv.addObject("weekList", pageBean.getList());
        mv.addObject("week", week);
        mv.addObject("page", pageBean.getPageNum());
        mv.addObject("pages", pageBean.getPages());
        mv.addObject("total", pageBean.getTotal());
        return mv;
    }

    //编辑工作量
    @RequestMapping("weekEdit.emp")
    public ModelAndView weekEdit(long weekId, @ModelAttribute("userLogin")XzEmp emp) {
        ModelAndView mv = new ModelAndView("backEnd/weekEdit");
        XzWeek week;
        if (weekId == 0) {
            week = new XzWeek();
            week.setEmpId(emp.getEmpId());
            week.setWeekId(0L);
        }else week = weekService.selById(weekId);
        mv.addObject("empList",empService.selAll(new XzEmp()));
        mv.addObject("projectList",projectService.selAll(new XzProject()));
        mv.addObject("week", week);
        return mv;
    }
    //修改工作量
    @RequestMapping("updateWeek.emp")
    public ModelAndView updateWeek(XzWeek week,@ModelAttribute("userLogin")XzEmp emp){
        ModelAndView mv = errors(week);
        if (mv.getViewName().equals("backEnd/workEdit"))return mv;
        week.setEmpId(emp.getEmpId());
        week.setEmp(emp);
        weekService.update(week);
        mv.addObject("projectList",projectService.selAll(new XzProject()));
        mv.addObject("week",week);
        return mv;
    }

    @RequestMapping("selById.emp")
    public ModelAndView selById(long weekId){
        ModelAndView mv =new ModelAndView("backEnd/weekShow");
        XzWeek week = weekService.selById(weekId);
        mv.addObject("week1",week);
        mv.addObject("week",new XzWeek());
        mv.addObject("open",1);
        return mv;
    }
    //删除工作量
    @RequestMapping("deleteWeek.emp")
    public ModelAndView deleteWeek(Long weekId){
        ModelAndView mv =new ModelAndView("redirect:selWeekAll.emp");
        weekService.isdelete(weekId);
        return mv;
    }
}
